/*
 * @Description: vite.config.ts vite2.0配置
 * @Version: 2.0
 * @Autor: lhl
 * @Date: 2021-04-03 23:18:54
 * @LastEditors: lhl
 * @LastEditTime: 2021-05-09 19:52:25
 */
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import viteCompression from 'vite-plugin-compression'
import path from 'path'
const resolve = (dir: string) => path.join(__dirname, dir)
// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    // 生产环境生成 .gz 文件
    viteCompression({
      verbose: true,
      disable: false,
      threshold: 10240,
      algorithm: 'gzip',
      ext: '.gz'
    })
  ],
  base: './', // 打包路径
  resolve: {
    // 设置别名
    alias: {
      '@': resolve('src'),
      Components: resolve('src/components'),
      Assets: resolve('src/assets'),
      // 解决vue-i18n警告You are running the esm-bundler build of vue-i18n. It is recommended to configure your bundler to explicitly replace feature flag globals with boolean literals to get proper tree-shaking in the final bundle.
      'vue-i18n': 'vue-i18n/dist/vue-i18n.cjs.js'
    }
  },
  css: {
    preprocessorOptions: {
      less: {
        modifyVars: {
          // Used for global import to avoid the need to import each style file separately
          // reference:  Avoid repeated references
          hack: `true; @import (reference) "${resolve('src/assets/style/global.less')}";`
        },
        javascriptEnabled: true
      }
    }
  },
  server: {
    host: '0.0.0.0',
    https: false,
    port: 4000, // 启动端口
    open: true,
    proxy: {
      // 选项写法
      '/api': {
        target: 'http://localhost:3000',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api/, '')
      }
    },
    cors: true
  },
  build: {
    // 生产环境移除 console
    terserOptions: {
      compress: {
        drop_console: true,
        drop_debugger: true
      }
    }
  }
})
